package com.ruoyi.category.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.category.mapper.CategoryMapper;
import com.ruoyi.category.domain.Category;
import com.ruoyi.category.service.ICategoryService;

import javax.annotation.Resource;

/**
 * 分类管理Service业务层处理
 * 
 * @author dxp
 * @date 2024-02-13
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ISysDictDataService sysDictDataService;
    @Resource
    private RedisTemplate<String,Category> redisTemplate;

    /**
     * 查询分类管理
     * 
     * @param id 分类管理主键
     * @return 分类管理
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询分类管理列表
     * 
     * @param category 分类管理
     * @return 分类管理
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增分类管理
     * 
     * @param category 分类管理
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        int i = categoryMapper.insertCategory(category);
        List<Category> categories = categoryMapper.selectCategoryList(category);
        Long id = categories.get(0).getId();
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("category");
        sysDictData.setDictLabel(category.getName());
        sysDictData.setDictSort(category.getSort());
        sysDictData.setDictValue(String.valueOf(id));
        sysDictData.setListClass("default");
        sysDictDataService.insertDictData(sysDictData);
        redisTemplate.delete("categoryList");
        return i;
    }

    /**
     * 修改分类管理
     * 
     * @param category 分类管理
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictValue(String.valueOf(category.getId()));
        sysDictData.setDictType("category");
        List<SysDictData> sysDictData1 = sysDictDataService.selectDictDataList(sysDictData);
        SysDictData sysDictData2 = sysDictData1.get(0);
        sysDictData2.setDictLabel(category.getName());
        sysDictData2.setDictSort(category.getSort());
        sysDictDataService.updateDictData(sysDictData2);
        redisTemplate.delete("categoryList");
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除分类管理
     * 
     * @param ids 需要删除的分类管理主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        for (Long id : ids) {
            deleteCategoryById(id);
        }
        redisTemplate.delete("categoryList");
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除分类管理信息
     * 
     * @param id 分类管理主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictValue(String.valueOf(id));
        sysDictData.setDictType("category");
        List<SysDictData> sysDictData1 = sysDictDataService.selectDictDataList(sysDictData);
        SysDictData sysDictData2 = sysDictData1.get(0);
        Long[] ids = {sysDictData2.getDictCode()};
        sysDictDataService.deleteDictDataByIds(ids);
        redisTemplate.delete("categoryList");
        return 1;
    }
}
